<?php
class Form_Sort extends Form_Base
{
    protected $_type;
    protected $_data;
    protected $_group_level;
    protected $_group_count;

    public function __construct ($type)
    {
        $this->_type = $type;
        $this->_data = array();
        $this->_group_level = 0;
        $this->_group_count = array();
    }

    public function build_list ($arr_where)
    {
        $primary_key = $this->primary_key(); 
        $primary_tbl = $this->primary_table();
        $arr_order_by= $this->order_by();

        $query = DB::select()
            ->from('form_list_config')
            ->where('name', '=', $this->_type)
            ->order_by('sort', 'asc')
            ->execute();

        $column_keys = array();
        foreach ($query as $rs)
        {
            $key = $rs['column_key'];
            $name = $rs['column_name'];
            $type = $rs['type'];
            $column_keys[$key] = array('name' => $name, 'type' =>$type);
        }


        $select_columns = array_keys($column_keys);
        if (! in_array($primary_key, $select_columns))
            $select_columns[] = $primary_key;

        $rows   = array();
        $query  = DB::select();
        foreach ($select_columns as $key)
            $query->select($key);
        $query->select('sort');

        $query->from($primary_tbl);

        foreach ($arr_where as $w)
        {
            $query->where($w[0], $w[1], $w[2]);
        }
            
        $query = $query->order_by($arr_order_by[0], $arr_order_by[1])
            ->execute();

        foreach ($query as $rs)
        {
            $rows[] = $rs;
        }

        $table = $this->build_table ($primary_key, $column_keys, $rows);
        $button = $this->build_buttons('create');
        //$categories = $this->build_categories($arr_where);
        $categories = '&nbsp;';

        $html  =<<<EOF
<div class="list {$this->_type}">
    <form action="" method="POST">
    <table class="gridtable">
        <tr>
        <td>设置排序：按数字大小倒序显示, 数字不得小于0</td>
        <td>$categories</td>
        </tr>
    </table>
    <br/>
    $table
    <div class="submit">
        <p><input type="submit" name="submit_op" value="设置排序"></p>
    </div>
    </form>
</div>

EOF;
        return $html;
    }

    protected function build_categories ($arr, $column_key = 'main_type')
    {
        $primary_tbl = $this->primary_table();
        $query = DB::select(DB::expr('distinct('.$column_key.') as cate'))
            ->from($primary_tbl)
            ->execute();

        $cates = array();
        $opts = '';
        $cate = $arr[0][2];
        foreach ($query as $rs)
        {
            $cates[] = $rs['cate']; 
            if ($rs['cate'] == $cate)
                $opts .= "<option value=\"{$rs['cate']}\" selected=\"selected\">{$rs['cate']}</option>";
            else
                $opts .= "<option value=\"{$rs['cate']}\">{$rs['cate']}</option>";
        }


        $controller = Request::initial()->controller();
        $directory  = Request::initial()->directory();

        $selects =<<<EOF
<script>
function on_cate_changed(item)
{
    cat = $(item).val();
    url = "/$directory/$controller/sort/" + cat;
    window.location.assign(url);
}
</script>
<select name="category" onchange="on_cate_changed(this)">
$opts
</select>
EOF;

        return $selects;

        
    }

    protected function build_table ($idKey, $column_names, $rows)
    {
        $ths = '';
        foreach ($column_names as $key => $rs)
        {
            $name = $rs['name'];
            $ths .= "\t<th>$name</th>\n";
        }

        $ths .= "\t<th>操作</th>\n";

        $head = "<tr>\n$ths</tr>\n";

        $content = '';
        foreach ($rows as $row)
        {
            $tds = '';
            $id  = Arr::get($row, $idKey, 0);
            foreach ($column_names as $key => $rs)
            {
                $name = $rs['name'];
                $ctyp = $rs['type'];
                $val  = Arr::get($row, $key, '');
                if ($key == 'created' || $key == 'changed')
                {
                    if (is_numeric($val))
                        $val = date("Y-m-d H:i:s", $val);
                }
                else if ($ctyp == 'image')
                {
                    $imgs = explode('|', $val);
                    $val  = $imgs[0];
                    $val = '<img src="'.$val.'" height="24px">';
                }

                $tds .= "\t<td>$val</td>\n";
            }
            $opts = $this->build_action_list($id, $row);
            $tds .= "\t<td>$opts</td>\n";

            $tr = "<tr>$tds</tr>\n";
            $content .= $tr;
        }

        $table =<<<EOF
<table class="gridtable">
    $head
    $content
</table>

EOF;

        return $table;
    }

    protected function build_action_list ($id, $row)
    {
        $controller = Request::initial()->controller();
        $directory  = Request::initial()->directory();

        return '权重：<input size="2" type="text" name="sort'.$id.'" value="'.$row['sort'].'">';
    }
}
